package cn.explink.dop.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import cn.explink.dop.entity.vo.RestaurantVO;
import cn.explink.dop.enums.pub.EnableEnum;
import cn.explink.dop.tools.Tools;
import cn.explink.eap.core.common.dao.impl.GenericBaseCommonDao;

/**
 * 餐厅管理dao层
 *
 * @author firefly 2015-2-26 14:35
 *
 */
@Repository("restaurantDao")
public class RestaurantDao extends GenericBaseCommonDao<RestaurantVO, String> {

	/**
	 * 修改基本信息
	 *
	 * @param baseInfo
	 *            基本信息对象
	 */
	public void modify(final RestaurantVO res) {
		StringBuffer sql = new StringBuffer("update bd_restaurant set ");
		if (StringUtils.isNotBlank(res.getResName())) {
			sql.append("res_name=?");
		}
		if (StringUtils.isNotBlank(res.getResDesc())) {
			sql.append(",res_name=? ");
		}
		sql.append(" ").append("where id=? ");
		this.getSession().update(res);
		// super.getJdbcTemplate().update(sql.toString());
	}

	public RestaurantVO getById(String restaurantId) {
		Query query = this.getSession().createQuery(" from RestaurantVO where id= :restaurantId");
		query.setString("restaurantId", restaurantId);
		return (RestaurantVO) query.uniqueResult();
	}

	/**
	 * 按照实体查询，无分页
	 *
	 * @param res
	 *            查询实体
	 * @return 查询列表
	 */
	public List<RestaurantVO> finds(final RestaurantVO res) {
		StringBuffer sql = new StringBuffer("select * from bd_restaurant where res_enable=").append(EnableEnum.Enable.getValue());
		if (StringUtils.isNotBlank(res.getResName())) {
			sql.append(" and res_name='").append(res.getResName()).append("'");
		}
		List<Map<String, Object>> mapList = super.getJdbcTemplate().queryForList(sql.toString());
		List<RestaurantVO> resList = new ArrayList<RestaurantVO>();
		for (Map<String, Object> m : mapList) {
			RestaurantVO r = (RestaurantVO) Tools.convertMap(RestaurantVO.class, m);
			resList.add(r);
		}
		return resList;
	}

	/**
	 * 按照实体获取分页数据
	 *
	 * @param baseInfo
	 *            查询实体
	 * @param pageNum
	 *            当前页
	 * @param pageSize
	 *            当前页记录条数
	 * @return 查询结果集
	 */
	public Map<String, Object> findByPage(final RestaurantVO res, final int pageNum, final int pageSize) {
		StringBuffer sql = new StringBuffer("select * ").append("from bd_restaurant where 1=1 ");
		// sql.append(EnableEnum.Enable.getValue());
		List<Object> params = new ArrayList<Object>();
		if (StringUtils.isNotBlank(res.getResName())) {
			sql.append(" and res_name like ? ");
			params.add("%" + res.getResName() + "%");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count", super.getJdbcTemplate().queryForLong(sql.toString().replace("*", "count(id)"), params.toArray()));
		sql.append(" limit ").append((pageNum - 1) * pageSize).append(",").append(pageSize);
		List<Map<String, Object>> mapList = super.getJdbcTemplate().queryForList(sql.toString(), params.toArray());
		List<RestaurantVO> resList = new ArrayList<RestaurantVO>();
		for (Map<String, Object> m : mapList) {
			RestaurantVO r = (RestaurantVO) Tools.convertMap(RestaurantVO.class, m);
			resList.add(r);
		}
		map.put("list", resList);
		return map;
	}

	/**
	 * 删除选中对象
	 *
	 * @param baseInfoList
	 *            参数集合
	 */
	public int delete(final List<RestaurantVO> resList) {
		StringBuffer sql = new StringBuffer("update bd_restaurant set ");
		sql.append("res_enable=").append(EnableEnum.Unable.getValue());
		TreeSet<String> set = new TreeSet<String>();
		for (RestaurantVO r : resList) {
			set.add("'" + r.getId() + "'");
		}
		sql.append(" ").append("where id in (").append(StringUtils.join(set.toArray(), ",")).append(")");
		return super.getJdbcTemplate().update(sql.toString());
	}

	/**
	 *
	 * @Title: checkResName
	 * @Description: 判断餐厅是否已经存在
	 * @author 孟凡岭
	 * @param @param resName
	 * @param @return
	 * @return List<Map<String,Object>> 返回类型
	 * @throws
	 */
	public List<Map<String, Object>> checkResName(String resName) {
		// TODO Auto-generated method stub
		String sql = "select id from bd_restaurant where res_name=?";
		return this.getJdbcTemplate().queryForList(sql, resName);
	}

	/**
	 *
	 * @Title: stopRows
	 * @Description: 停用餐厅
	 * @author 孟凡岭
	 * @param @param ids
	 * @return void 返回类型
	 * @throws
	 */
	public void stopRows(String ids) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer("update bd_restaurant set ");
		sql.append("res_enable=").append(EnableEnum.Unable.getValue());
		sql.append(" ").append("where id in (").append(ids).append(")");
		super.getJdbcTemplate().update(sql.toString());
	}

	/**
	 *
	 * @Title: openRows
	 * @Description: TODO
	 * @author 孟凡岭
	 * @param @param ids
	 * @return void 返回类型
	 * @throws
	 */
	public void openRows(String ids) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer("update bd_restaurant set ");
		sql.append("res_enable=").append(EnableEnum.Enable.getValue());
		sql.append(" ").append("where id in (").append(ids).append(")");
		super.getJdbcTemplate().update(sql.toString());

	}

}
